import { Module } from 'vuex'
import { getResource } from '@/api/resource'

interface kv {
  label: string;
  value: string;
}
export interface TerminalState {
  data: kv[]
}
// eslint-disable-next-line
const terminal: Module<TerminalState, any> = {
  namespaced: true,
  state: () => ({
    data: []
  }),
  getters: {
    getTerminal (state) {
      return state.data
    },
    getName (state) {
      return (key: string) => state.data.find(item => item.value === key)?.label
    },
  },
  mutations: {
    SET_TERMINAL (state, payload) {
      state.data = payload
    }
  },
  actions: {
    getDict ({ commit }) {
      return new Promise((resolve, reject) => {
        getResource('terminal').then(res => {
          if (res && res.data) {
            const  list  = res.data?.properties?.meta
            commit('SET_TERMINAL', list)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(reject)
      })
    }
  }
}

export default terminal
